package org.go.smart.settings;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.go.smart.JSONUtil;

@SuppressWarnings("serial")
public class SettingsServlet extends HttpServlet{
	    
	    public void init(ServletConfig config) throws ServletException {
	    	super.init(config);
	    }

	    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
	       String tempThr = request.getParameter("temperatureThreshold");
	       if (tempThr != null) {
	    	   SettingsBean.getInstance().setTemperatureThreshold(Double.parseDouble(tempThr));
	    	   response.setStatus(HttpServletResponse.SC_OK);
	    	   response.getWriter().append("Temperature threshold has been successfully set to " + tempThr);
	       }
	    }

	    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	        response.setContentType("text/json; charset=utf-8");
	        ServletOutputStream out = response.getOutputStream();
	        out.println(JSONUtil.buildSettings());
	        out.flush();
	    }

}
